import 'package:flutter/material.dart';
import '../constants/hand_drawn_input_icon_type.dart';

class HandDrawnInputIcon extends StatelessWidget {
  final HandDrawnInputIconType type;
  final double size;
  final Color color;

  const HandDrawnInputIcon({
    Key? key,
    required this.type,
    this.size = 24,
    this.color = Colors.black,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size(size, size),
      painter: _HandDrawnInputIconPainter(
        type: type,
        color: color,
      ),
    );
  }
}

class _HandDrawnInputIconPainter extends CustomPainter {
  final HandDrawnInputIconType type;
  final Color color;

  _HandDrawnInputIconPainter({
    required this.type,
    required this.color,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2.0
      ..strokeCap = StrokeCap.round;

    final center = Offset(size.width / 2, size.height / 2);
    final radius = size.width / 2 - 4;

    switch (type) {
      case HandDrawnInputIconType.mic:
        _drawMicIcon(canvas, size, paint);
        break;
      case HandDrawnInputIconType.image:
        _drawImageIcon(canvas, size, paint);
        break;
    }
  }

  void _drawMicIcon(Canvas canvas, Size size, Paint paint) {
    // 画麦克风主体（圆形）
    final path = Path();
    path.addOval(Rect.fromCircle(
      center: Offset(size.width / 2, size.height / 2),
      radius: size.width * 0.25,
    ));
    canvas.drawPath(path, paint);

    // 画麦克风支架
    final standPath = Path();
    standPath.moveTo(size.width * 0.4, size.height * 0.6);
    standPath.lineTo(size.width * 0.4, size.height * 0.8);
    standPath.lineTo(size.width * 0.6, size.height * 0.8);
    standPath.lineTo(size.width * 0.6, size.height * 0.6);
    canvas.drawPath(standPath, paint);

    // 画声波线（更简单的波浪）
    final wavePath = Path();
    wavePath.moveTo(size.width * 0.2, size.height * 0.5);
    wavePath.quadraticBezierTo(
      size.width * 0.5, size.height * 0.3,
      size.width * 0.8, size.height * 0.5
    );
    canvas.drawPath(wavePath, paint);
  }

  void _drawImageIcon(Canvas canvas, Size size, Paint paint) {
    // 画图片框（圆角矩形）
    final path = Path();
    path.addRRect(RRect.fromRectAndRadius(
      Rect.fromLTWH(
        size.width * 0.2,
        size.height * 0.2,
        size.width * 0.6,
        size.height * 0.6,
      ),
      Radius.circular(size.width * 0.1),
    ));
    canvas.drawPath(path, paint);

    // 画图片内容（简单的山形图案）
    final contentPath = Path();
    contentPath.moveTo(size.width * 0.3, size.height * 0.6);
    contentPath.lineTo(size.width * 0.5, size.height * 0.3);
    contentPath.lineTo(size.width * 0.7, size.height * 0.6);
    canvas.drawPath(contentPath, paint);

    // 画太阳
    canvas.drawCircle(
      Offset(size.width * 0.5, size.height * 0.4),
      size.width * 0.08,
      paint,
    );
  }

  @override
  bool shouldRepaint(_HandDrawnInputIconPainter oldDelegate) {
    return oldDelegate.type != type || oldDelegate.color != color;
  }
} 